import wallet from "./common/wallet";
import utils from "./common/utils"
import request from "./common/request";

let init = {};

init.initAddress = async (from) => {
    const address = await wallet.getAddress();
    console.log(address, from);
    await request("/front/getLoginCode", {
        address: address
    }).then(async res => {
        if (res.code === 0) {
            await wallet.signMessage(res.data).then(async signRes => {
                await request("/front/login", {
                    address: address,
                    signature: signRes
                }).then(loginRes => {
                    if (loginRes.code === 0) {
                        uni.setStorageSync("localeAddress", address)
                        uni.setStorageSync("token", loginRes.data)
                    } else {
                        utils.toast(loginRes.msg)
                    }
                })
            })
        } else {
            utils.toast(res.msg)
        }
    })
}

export default init;
